/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include "task_relay.h"

#define RELAY_PIN		GPIO_Pin_14
#define RELAY_NUM		GPIO_NUM_14
#define RELAY_MODE		GPIO_MODE_OUTPUT

#define RELAY_SIG_PIN		GPIO_Pin_12
#define RELAY_SIG_NUM		GPIO_NUM_12
#define RELAY_SIG_MODE		GPIO_MODE_INPUT

void task_relay_init(void)
{
	gpio_config_t gpio_config_structure;

	gpio_config_structure.pin_bit_mask = RELAY_PIN;
	gpio_config_structure.mode = RELAY_MODE;
	gpio_config_structure.pull_up_en = 1;
	gpio_config_structure.pull_down_en = 0;
	gpio_config_structure.intr_type = GPIO_INTR_DISABLE;
	gpio_config(&gpio_config_structure);


	gpio_config_structure.pin_bit_mask = RELAY_SIG_PIN;
	gpio_config_structure.mode = RELAY_SIG_MODE;
	gpio_config_structure.pull_up_en = 1;
	gpio_config_structure.pull_down_en = 0;
	gpio_config_structure.intr_type = GPIO_INTR_DISABLE;
	gpio_config(&gpio_config_structure);

#if 0
	relay_active(RELAY_ON);
#else
	if (FRAM_SAVE_DATA_NAME____DIDO_t_.BITS.Relay_ShouldRecover)
	{
		if (FRAM_SAVE_DATA_NAME____DIDO_t_.BITS.Relay_OnOFF)
		{
			relay_active(RELAY_ON);
		}
		else
		{
			relay_active(RELAY_OFF);
		}
	}
	else
	{
		if (FRAM_SAVE_DATA_NAME____DIDO_t_.BITS.Relay_Def)
		{
			relay_active(RELAY_ON);
		}
		else
		{
			relay_active(RELAY_OFF);
		}
	}
#endif

}

void relay_task(void* parm)
{

	while(1)
	{
		vTaskDelay( 500 / portTICK_PERIOD_MS );
	}

}


void relay_active(relay_onoff onoff)
{
	if (onoff == RELAY_ON) {
		gpio_set_level(RELAY_NUM, 1);
	}
	else {
		gpio_set_level(RELAY_NUM, 0);
	}
}

relay_onoff relay_get(void)
{
#if (0)
	return gpio_get_level(RELAY_NUM);
#else
	int level = gpio_get_level(RELAY_SIG_NUM);
	if (level)
		return RELAY_OFF;
	else
		return RELAY_ON;
#endif
}
relay_onoff relay_get_setVal(void)
{
	int level =  gpio_get_level(RELAY_NUM);
	if (level)
		return RELAY_ON;
	else
		return RELAY_OFF;
}

